package com.smartglass.alfareallive;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import com.ingenic.glass.api.touchboard.GestureDetector;

/**
 * Created by victor on 2016/6/23.
 */
public class AlfaLiveDialog extends Dialog {

    private GestureDetector mGestureDetector;
    private AlfaLiveDialogOnTapListener mAlfaLiveDialogOnTapListener;
    private Activity mActivity;
    public AlfaLiveDialog(Activity mActivity) {
        super(mActivity);
        this.mActivity = mActivity;
        mGestureDetector = new GestureDetector(mActivity, new MySimpleGestureDetectorListener());
        initSetting();
    }

    public AlfaLiveDialog(Activity mActivity, int theme) {
        super(mActivity, theme);
        this.mActivity = mActivity;
        mGestureDetector = new GestureDetector(mActivity, new MySimpleGestureDetectorListener());
        initSetting();
    }

    private void initSetting() {
        this.setCanceledOnTouchOutside(true);
        this.setCancelable(true);
        WindowManager.LayoutParams lay = this.getWindow().getAttributes();
        setParams(lay);
    }

    /**
     * 设置dialog参数
     * @param lay
     */
    private void setParams(WindowManager.LayoutParams lay) {
        DisplayMetrics dm = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        Rect rect = new Rect();
        View view = getWindow().getDecorView();
        view.getWindowVisibleDisplayFrame(rect);
        lay.height = dm.heightPixels - rect.top;
        lay.width = dm.widthPixels;
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }

    interface AlfaLiveDialogOnTapListener{
        void dialogOnTap();
    }

    protected void setAlfaLiveDialogOnTapListener(AlfaLiveDialogOnTapListener mAlfaLiveDialogOnTapListener){
        this.mAlfaLiveDialogOnTapListener = mAlfaLiveDialogOnTapListener;
    }

    class MySimpleGestureDetectorListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onSlideLeft(boolean fromPhone) {
            return true;
        }

        @Override
        public boolean onSlideDown(boolean fromPhone) {

            return true;
        }

        @Override
        public boolean onTap(boolean fromPhone) {
            Log.d("AlfaLive", "onTap");
            if(mAlfaLiveDialogOnTapListener != null){
                mAlfaLiveDialogOnTapListener.dialogOnTap();
            }
            return true;
        }

    }
}
